import { onBeforeMount, onBeforeUnmount, computed, ref } from "vue";
import { eventBus } from "@visual/utils";

const useEvents = ({ curComp, curConfig }) => {
  //订阅事件
  onBeforeMount(() => {
    //所有监听的事件都会执行，这是一个全量的广播，放到这里和写到组件内部的区别是啥

    eventBus.on("compClick", compClick);

    eventBus.on("compFilter", compFilter);
    eventBus.on("compRouter", compRouter);
    eventBus.on("compDialog", compDialog);

    console.log("onBeforeMount");
  });
  onBeforeUnmount(() => {
    offEvent("compClick");
    offEvent("compFilter");
    offEvent("compRouter");
    offEvent("compDialog");

    console.log("onBeforeUnmount");
  });
  const showFilter = ref(true);
  const showCondition = computed(() => {
    return showFilter.value;
  });
  const compClick = () => {};

  const compFilter = ({ emitConfig, emitParams }) => {
    console.log("compFilter", curComp, curConfig, emitConfig, emitParams);
    const filterContion = curConfig.propValue.filter;
    const compEmitData = emitParams["compEmitData"];

    if (!compEmitData || !filterContion || filterContion?.indexOf(compEmitData) !== -1) {
      showFilter.value = true;
    } else {
      showFilter.value = false;
    }
  };
  const compRouter = ({ emitConfig, emitParams }) => {
    console.log("compRouter", emitConfig, emitParams);
  };
  const compDialog = ({ emitConfig, emitParams }) => {
    console.log("compDialog", emitConfig, emitParams);
  };
  function offEvent(type) {
    switch (type) {
      case "compClick":
        eventBus.off("compClick", compClick);
        break;
      case "compFilter":
        eventBus.off("compFilter", compFilter);
        break;
      case "compRouter":
        eventBus.off("compRouter", compRouter);
        break;
      case "compDialog":
        eventBus.off("compDialog", compDialog);
        break;
      default:
        break;
    }
  }
  return {
    offEvent,
    showCondition
  };
};
export default useEvents;
